package org;

import org.entities.LevelObject;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Color;
import org.newdawn.slick.Input;
import org.newdawn.slick.Image;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Font;

public class MapState  extends BasicGameState {

    private StateBasedGame game;
    private Image mapScreen;
    //private Image mapGrid;
    //private Image mapBg;
    private static int worldX;
    private static int worldY;
    public static boolean loopCtrl;
    public static boolean clrClip;
    private boolean up,down,left,right;
    private Font font;
    public static final int ID = 6;
    private static int moveX=0;
    private static int moveY=0;
    //private int currentPressTime = 0;    
    //private int maxPressTime = 100;      
    //private boolean nextPressOK;   
    private int loopX,loopY;
    private static String mapLoc;
    boolean drawbg;
    private Image mapFull,cube;

    public int getID() {
        return ID;
    }


    /*	
	 create a file reader to load map	
     */

    public void init(GameContainer container, StateBasedGame game) throws SlickException {

        this.game = game;		
        mapScreen = new Image("data/image/mapScreen3.png");
        //mapBg = new Image("data/image/titlebg.jpg");
        //mapGrid = new Image("data/image/mapGrid3.png");
        font = new AngelCodeFont("data/font/weasel.fnt","data/font/weasel.png");
        mapFull= new Image("data/image/mapFull.png");
        //mapMask= new Image("data/image/mapMask.png");
        cube= new Image("data/image/cube2.png");
    }

    public void update(GameContainer container, StateBasedGame game, int delta){
 
    }

    public static void registerRoom(){
        moveX=0;
        moveY=0;

        mapLoc = BlockMap.tmap.getMapProperty("mapLoc","0");

        for (int a=0;a<mapLoc.split("\\,").length;a+=2){
            worldX=Integer.parseInt(mapLoc.split("\\,")[a]);
            worldY = Integer.parseInt(mapLoc.split("\\,")[a+1]);
            if (!Progress.storeMap.contains(Progress.mapBlockAt[worldX][worldY])){
                Progress.storeMap.add(Progress.mapBlockAt[worldX][worldY] = new String("true"));                   
            }
            //System.out.printf("registering a %d tile sized room on the map\n",Progress.storeMap.size());
        }   
    }


    public void render(GameContainer container, StateBasedGame game, Graphics g) { 
        if (!LevelObject.debugMode)
        g.scale(2f, 2f);
        g.drawImage(mapScreen,0,0); 
        //g.drawImage(mapGrid,0,0);        
        g.translate(31, 63);
        
        for (int a=0;a<mapLoc.split("\\,").length;a+=2){ 
            int blehX=Integer.parseInt(mapLoc.split("\\,")[a]);
            int blehY= Integer.parseInt(mapLoc.split("\\,")[a+1]);
            //g.setDrawMode(Graphics.MODE_SCREEN);
            //g.setColor(new Color(72,168,56));
            g.setColor(Color.red);
            g.fillRect((blehX)*8, (blehY)*8, 10, 10);
        }
        g.resetTransform();
        if (!LevelObject.debugMode)
            g.scale(2f, 2f);
        
        g.clearAlphaMap();
        g.translate(32, 64);
        for (loopX=0;loopX<42;loopX++){
            for (loopY=0;loopY<26;loopY++){               

                if (getBlockAt(loopX,loopY)!=null&&getBlockAt(loopX,loopY).equals("true")){
                    cube.draw((loopX+moveX)*8, (loopY+moveY)*8, 8, 8);
                }  
            }
        }
        g.resetTransform();
        if (!LevelObject.debugMode)
        g.scale(2f, 2f);
        g.setDrawMode(Graphics.MODE_ALPHA_BLEND);
        mapFull.draw(32,64);
        g.setDrawMode(Graphics.MODE_NORMAL);
        g.setFont(font);
        g.setColor(Color.white);
        g.scale(0.6f, 0.6f);
        g.drawString(Progress.areaName, 273+(Progress.areaName.length()*8/2), -2);
        g.resetTransform();
    }

    public static String getBlockAt(int x, int y) {
        return Progress.mapBlockAt[x][y];
    }

    public void moveMap(){
        
        // rework this out later
        if (left) {
            if (!(worldX+moveX<6))
                moveX-=1;
        }
        if (right) {
            if (!(worldX+moveX>44))
                moveX+=1;
        }
        if (up) {
            if (!(worldY+moveY<9))
                moveY-=1;
        }
        if (down) {
            if (!(worldY+moveY>32))
                moveY+=1;
        }
        System.out.printf("worldX+moveX=[%d]\n",worldX+moveX);
        System.out.printf("worldY+moveY=[%d]\n",worldY+moveY);
    }

    public void keyReleased(int key, char c) {

        if (key == Input.KEY_LSHIFT) {
            game.enterState(GameState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
        }
        if (key == Input.KEY_TAB) {
            game.enterState(StatusState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
        }
        if (key == Input.KEY_SPACE) {
            drawbg=!drawbg;
        }
    }

}
